﻿#include <CRITICAL.HPP>
#include <SYSSELEC.HPP>


/*******************************
**         Win32 的實作       **
*******************************/
#if (SysSelect == _WIN32_CxxlMan)

#include <windows.h>

class ImpCriticalSec:public CxxlMan::ICriticalSec
{
  CRITICAL_SECTION m_cs;                // Win32 所用的關鍵區域變數
public:


  ImpCriticalSec(void)
  {
    InitializeCriticalSection(&m_cs);
  }

  ~ImpCriticalSec(void)
  {
    DeleteCriticalSection(&m_cs);
  }

  virtual void cxxlFASTCALL Enter(void)        // 進入關鍵區域
  {
    EnterCriticalSection(&m_cs);
  }

  virtual void cxxlFASTCALL Leave(void)        // 離開關鍵區域
  {
    LeaveCriticalSection(&m_cs);
  }
};


/*******************************
**         Linux 的實作       **
*******************************/
#elif (SysSelect == _LINUX_CxxlMan)

#include <pthread.h>

class ImpCriticalSec:public CxxlMan::ICriticalSec
{
  pthread_mutex_t m_cs;                // Linux 所用的關鍵區域變數
public:


  ImpCriticalSec(void)
  {
    pthread_mutex_init(&m_cs,NULL);
  }

  ~ImpCriticalSecBase(void)
  {
    pthread_mutex_destroy(&m_cs);
  }

  virtual void cxxlFASTCALL Enter(void)        // 進入關鍵區域
  {
    pthread_mutex_lock(&m_cs);
  }

  virtual void cxxlFASTCALL Leave(void)        // 離開關鍵區域
  {
    pthread_mutex_unlock(&m_cs);
  }
};



#else
#error 尚未支援此作業系統
#endif

CxxlMan::CriticalSec::CriticalSec(void)       // Constructor
{
  Imp = new ImpCriticalSec;
}

CxxlMan::CriticalSec::~CriticalSec(void)       // Destructor
{
  delete Imp;
}






